import { createStore } from 'vuex';
import axios from 'axios';

const api = axios.create({
  baseURL: 'http://localhost:8092',
});

export default createStore({
  state: {
    publicKey: '', // 存储公钥
  },
  mutations: {
    setPublicKey(state, publicKey) {
      state.publicKey = publicKey; // 设置公钥
    },
  },
  actions: {
    fetchPublicKey({ commit }) {
      return api.get('/api/v1/rsa/public-key')
        .then(response => {
          commit('setPublicKey', response.data); // 存储公钥
        })
        .catch(error => {
          console.error('生成公钥失败:', error);
          throw error;
        });
    },
  },
  getters: {
    publicKey: (state) => state.publicKey, // 获取公钥
  },
});